﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace CFR.Silverlight.Core
{
    public static class DependencyObjectExtender
    {
        public static List<DependencyObject> GetAllChildren(this DependencyObject instance)
        {
            return GetAllChildren(instance, true);
        }
        public static List<DependencyObject> GetAllChildren(this DependencyObject instance, bool recursive)
        {
            if (instance == null)
                return new List<DependencyObject>();

            int count = VisualTreeHelper.GetChildrenCount(instance);
            List<DependencyObject> result = new List<DependencyObject>(count);

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(instance, i);
                    result.Add(child);

                    if (recursive)
                        result.AddRange(child.GetAllChildren(recursive));
                }
            }

            return result;

        }
    }
}
